package com.win.config;

import com.win.interceptor.JwtTokenInterceptor;
import com.win.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    private JwtTokenInterceptor jwtTokenInterceptor;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");

        registry.addInterceptor(jwtTokenInterceptor)
                .excludePathPatterns("/users/login")
                .excludePathPatterns("/users/register")
                .excludePathPatterns("/users/checkUser");

    }
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("开始注册自定义消息转换器...");
        //创建自己的消息转换器对象
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //为当前消息转换器添加一个对象转换器(用于转换java对象为json格式的数据)
        converter.setObjectMapper(new JacksonObjectMapper());

        //将该消息转换器对象添加到IO容器中由SpringMVC管理
        //由于容器中还有别的消息转换器对象，因此，将我们自定义的消息转换器索引设置为0，优先使用我们自己的消息转换器
        converters.add(0,converter);
    }
}
